package top.ljl.crawler.utils;

import org.json.JSONObject;
import org.springframework.stereotype.Component;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class GetJson {
    public JSONObject getHttpJson(String url){
        try {
            URL realUrl = new URL(url);
            HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();
            //设置HTTP的请求头(参考材料准备截图的请求头信息)
            connection.setRequestProperty("Accept", "*/*");//设置浏览器可以接收的媒体类型
            connection.setRequestProperty("Connection", "keep-alive");//网页打开，建立连接
            connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3928.4 Safari/537.36");//客户端使用的操作系统和浏览器的名称和版本
            // 建立实际的连接
            connection.connect();
            //请求成功
            if (connection.getResponseCode() == 200) {
                InputStream is = connection.getInputStream();
                //创建字节数组输出流对象
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                //10MB的缓存
                byte[] buffer = new byte[10485760];
                int len = 0;
                while ((len = is.read(buffer))!= -1) {
                    byteArrayOutputStream.write(buffer, 0, len);
                }
                String jsonString = byteArrayOutputStream.toString();
                byteArrayOutputStream.close();
                is.close();
                //转换成json数据返回
                return new JSONObject(jsonString);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}